'''
定义接口测试用例
1.导包
2.创建测试类 (集成unittest.TestCase)
    2.1前置处理（获取链接地址
    2.2后置处理（关闭需要关闭的实例化对象
    2.3 创建测试方法
'''
import unittest
import json
import app
from parameterized import parameterized

from api.login import LoginApi


def built_data():
    # 创建一个存放数据的list数组
    test_data = []
    # 读取文件信息
    with open(app.BASE_DIR + "/data/loginData.json", "r", encoding="utf-8") as f:
        # 加载json数据
        json_data = json.load(f)
        # 读取json内的数据
        for data_case in json_data:
            mobile = data_case.get("mobile")
            password = data_case.get("password")
            status_code = data_case.get("status_code")
            success = data_case.get("success")
            code = data_case.get("code")
            message = data_case.get("message")
            # append 在最后面添加一个数据，将括号内的数据作为一个整体，直接返回
            test_data.append((mobile, password, status_code, success, code, message))
        return test_data


class TestLogin(unittest.TestCase):
    def setUp(self):
        self.login_api = LoginApi()

    def tearDown(self):
        pass

    @parameterized.expand(built_data())
    def test01_login(self, mobile, password, status_code, success, code, message):
        response = self.login_api.login_i(mobile, password)
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(success, response.json().get("success"))
        self.assertEqual(code, response.json().get("code"))
        self.assertEqual(message, response.json().get("message"))

